﻿#include "dialog.h"

GameDialog dialogs;

GameDialog::GameDialog()
{
}

GameDialog::~GameDialog()
{
	dialog_actions.clear();
}

// Инициализация текстовых ресурсов
void GameDialog::Init(CL_GraphicContext &gc, CL_VirtualDirectory vdir, float stime, float atime, CL_Font_Freetype* rfont, CL_Font_Freetype* jfont)
{
	dialog_res.load("text.xml", vdir);
	starting_time = stime;
	char_appear_time = atime;
	dialog_frame = sprite_container.GetSprite("dialog_frame");
	// Шрифты
	russian_font = rfont;
	japanese_font = jfont;

	lpos = 0;
	rpos = 220.0f;
	vpos = 120.0f;
	hsize = 228.0f;
	vsize = 440.0f;

	llpos = 60.0f;
	l1rpos = 383.0f;
	l2rpos = 408.0f;
	l3rpos = 433.0f;
	l1jpos = 385.0f;
	l2jpos = 410.0f;
	l3jpos = 435.0f;
}

// Загрузка диалога и начало
void GameDialog::StartDialog(CL_String name)
{
	if (!dialog_res.resource_exists(name))
	{
		global_state.game_dialog = 0;
		return;
	}

	dialog_state = 0;
	dialog_time = 0;
	left_char = 0;
	right_char = 0;
	show_pic = 0;
	current_action = 0;
	dialog_actions.clear();
	line1 = "";
	line2 = "";
	line3 = "";

	CL_DomElement element = dialog_res.get_resource(name).get_element();
	std::vector<CL_DomNode> a_nodes = element.select_nodes("action");
	for (size_t i = 0; i < a_nodes.size(); i++)
	{
		CL_DomElement a_element = a_nodes[i].to_element();
		DialogAction new_action;
		new_action.text = a_element.get_text();
		if (a_element.get_attribute("name") == "new_line")
			new_action.atype = 0;
		if (a_element.get_attribute("name") == "line2")
			new_action.atype = 1;
		if (a_element.get_attribute("name") == "line3")
			new_action.atype = 2;
		if (a_element.get_attribute("name") == "wait")
			new_action.atype = 5;
		if (a_element.get_attribute("name") == "feed_char_left")
			new_action.atype = 10;
		if (a_element.get_attribute("name") == "feed_char_right")
			new_action.atype = 11;
		if (a_element.get_attribute("name") == "show_pic")
			new_action.atype = 20;
		if (a_element.get_attribute("name") == "hide_pic")
			new_action.atype = 21;
		if (a_element.get_attribute("name") == "set_char_face")
		{
			new_action.atype = 100;
			new_action.text = a_element.get_attribute("char");
			new_action.int_param = CL_StringHelp::text_to_int(a_element.get_text());
		}
		if (a_element.get_attribute("name") == "level_end")
			new_action.atype = 150;
		dialog_actions.push_back(new_action);
	}
}

void GameDialog::UpdateDialog(float time, int key_fire)
{
	dialog_time += time;

	// 0 - Развёртывание поля для сообщений
	if (dialog_state == 0)
	{
		if (dialog_time > starting_time)
			dialog_state = 1;
	}
	// 1 - Обработка действий диалога
	if (dialog_state == 1)
	{
		if (current_action >= dialog_actions.size())
		{
			// Отработали все действия, можно свернуть диалог
			dialog_state = 999;
			time_mark = dialog_time;
			return;
		}
		// Определим тип действия
		if (dialog_actions[current_action].atype == 0)
		{
			// Новая строка
			line1 = dialog_actions[current_action].text;
			line2 = "";
			line3 = "";
		}
		else if (dialog_actions[current_action].atype == 1)
		{
			// Вторая строка
			line2 = dialog_actions[current_action].text;
		}
		else if (dialog_actions[current_action].atype == 2)
		{
			// Третья строка
			line3 = dialog_actions[current_action].text;
		}
		else if (dialog_actions[current_action].atype == 5)
		{
			// Ожидание ввода
			dialog_state = 5;
			wait_time = 10000.0; // ждать 10 секунд до автопромотки
		}
		else if (dialog_actions[current_action].atype == 10)
		{
			// Ввод персонажа слева
			if (dialog_actions[current_action].text == "aya")
			{
				left_char = 1;
				left_sprite = sprite_container.GetSprite("char_sprite_aya1");
				time_mark = dialog_time;
				dialog_state = 10;
			}
		}
		else if (dialog_actions[current_action].atype == 11)
		{
			// Ввод персонажа справа
			if (dialog_actions[current_action].text == "aya")
			{
				right_char = 1;
				right_sprite = sprite_container.GetSprite("char_sprite_aya1");
				time_mark = dialog_time;
				dialog_state = 11;
			}
		}
		else if (dialog_actions[current_action].atype == 20)
		{
			// Показать картинку
			pic_sprite = sprite_container.GetSprite(dialog_actions[current_action].text);
			show_pic = 1;
		}
		else if (dialog_actions[current_action].atype == 21)
		{
			// Скрыть картинку
			show_pic = 0;
		}
		else if (dialog_actions[current_action].atype == 100)
		{
			// Смена кадра портрета персонажа
			CL_Sprite *tmp_sprite;
			if (dialog_actions[current_action].text == "aya")
				tmp_sprite = sprite_container.GetSprite("char_sprite_aya1");
			else if (dialog_actions[current_action].text == "aya")
				tmp_sprite = sprite_container.GetSprite("char_sprite_aya1");
			if (!tmp_sprite->is_null())
				tmp_sprite->set_frame(dialog_actions[current_action].int_param);
		}
		else if (dialog_actions[current_action].atype == 150)
		{
			// конец уровня
			//dialog_state = 999;
			global_state.game_level_end = 1;
			return;
		}
		current_action++;
	}
	if (dialog_state == 5)
	{
		// Ожидание ввода
		wait_time -= time;
		if (wait_time < 0)
			dialog_state = 1;
		if (key_fire)
			dialog_state = 1;
	}
	if (dialog_state == 10 || dialog_state == 11)
	{
		// Ввод персонажа
		if (dialog_time > (time_mark + char_appear_time))
			dialog_state = 1;
	}
	if (dialog_state == 999)
	{
		// Завершение диалога
		if (dialog_time - time_mark > char_appear_time)
			global_state.game_dialog = 0;
	}
}

void GameDialog::RenderDialog(CL_GraphicContext &gc)
{
	gc.set_cliprect(global_state.clip_rect);
	// Показать картинку, если нужно
	if (show_pic && !pic_sprite->is_null())
	{
		pic_sprite->set_alignment(origin_center);
		pic_sprite->draw(gc, 224, 200);
	}
	// Рисуем Спрайты персонажей
	if (dialog_state == 1 || dialog_state == 5)
	{
		// Отрисовать текущее состояние
		if (left_char)
		{
			left_sprite->set_angle_yaw(CL_Angle(0.0f, cl_degrees));
			left_sprite->draw(gc, lpos, vpos);
		}
		if (right_char)
		{
			right_sprite->set_angle_yaw(CL_Angle(180.0f, cl_degrees));
			right_sprite->draw(gc, rpos, vpos);
		}
	}
	else if (dialog_state == 10)
	{
		// появление слева
		if (right_char)
		{
			right_sprite->set_angle_yaw(CL_Angle(180.0f, cl_degrees));
			right_sprite->draw(gc, rpos, vpos);
		}
		left_sprite->set_angle_yaw(CL_Angle(0.0f, cl_degrees));
		left_sprite->draw(gc, lpos + hsize * ((dialog_time - time_mark) / char_appear_time) - hsize, vpos + vsize - ((dialog_time - time_mark) / char_appear_time) * vsize);
	}
	else if (dialog_state == 11)
	{
		// появление справа
		if (left_char)
		{
			left_sprite->set_angle_yaw(CL_Angle(0.0f, cl_degrees));
			left_sprite->draw(gc, lpos, vpos);
		}
		right_sprite->set_angle_yaw(CL_Angle(180.0f, cl_degrees));
		right_sprite->draw(gc, rpos + hsize - ((dialog_time - time_mark) / char_appear_time) * hsize, vpos + vsize - ((dialog_time - time_mark) / char_appear_time) * vsize);
	}
	else if (dialog_state == 999)
	{
		// Выключение диалога
		if (left_char)
		{
			left_sprite->set_angle_yaw(CL_Angle(0.0f, cl_degrees));
			left_sprite->draw(gc, lpos - hsize * ((dialog_time - time_mark) / char_appear_time), vpos + ((dialog_time - time_mark) / char_appear_time) * vsize);
		}
		if (right_char)
		{
			right_sprite->set_angle_yaw(CL_Angle(180.0f, cl_degrees));
			right_sprite->draw(gc, rpos + ((dialog_time - time_mark) / char_appear_time) * hsize, vpos + ((dialog_time - time_mark) / char_appear_time) * vsize);
		}
	}
	// Поверх - текстовое поле
	if (dialog_state == 0)
	{
		// Развёртывание поля для сообщений
		float a,sx;
		if (dialog_time < (starting_time / 2.0f))
		{
			a = (dialog_time / starting_time) * 2.0f;
			sx = a;
		}
		else
		{
			a = 1.0f;
			sx = 1.0f;
		}
		float sy = dialog_time / starting_time;
		dialog_frame->set_alpha(a);
		dialog_frame->set_scale(sx, sy);
		dialog_frame->draw(gc, 224.0f - (180.0f * sx), 355.0f);
	}
	else if (dialog_state == 999)
	{
		// тухнущее поле ввода
		// рамка
		float a = ((time_mark + char_appear_time - dialog_time) / char_appear_time);
		dialog_frame->set_alpha(a);
		dialog_frame->draw(gc, 44.0f, 355.0f);
		CL_Colorf t_colorb = CL_Colorf::black;
		CL_Colorf t_color = CL_Colorf::lightyellow;
		t_colorb.a = a;
		t_color.a = a;
		// Текст
		if (global_state.lang == 0)
		{
			// Русский
			russian_font->draw_text(gc, llpos + 1, l1rpos + 1, line1, t_colorb);
			russian_font->draw_text(gc, llpos + 1, l2rpos + 1, line2, t_colorb);
			russian_font->draw_text(gc, llpos + 1, l3rpos + 1, line3, t_colorb);
			russian_font->draw_text(gc, llpos, l1rpos, line1, t_color);
			russian_font->draw_text(gc, llpos, l2rpos, line2, t_color);
			russian_font->draw_text(gc, llpos, l3rpos, line3, t_color);
		}
		else
		{
			// Японский
			japanese_font->draw_text(gc, llpos + 1, l1jpos + 1, line1, t_colorb);
			japanese_font->draw_text(gc, llpos + 1, l2jpos + 1, line2, t_colorb);
			japanese_font->draw_text(gc, llpos + 1, l3jpos + 1, line3, t_colorb);
			japanese_font->draw_text(gc, llpos, l1jpos, line1, t_color);
			japanese_font->draw_text(gc, llpos, l2jpos, line2, t_color);
			japanese_font->draw_text(gc, llpos, l3jpos, line3, t_color);
		}
	}
	else
	{
		// Готовое поле с текстом
		dialog_frame->set_alpha(1.0);
		dialog_frame->set_scale(1.0, 1.0);
		dialog_frame->draw(gc, 44.0f, 355.0f);
		// Текст
		if (global_state.lang == 0)
		{
			// Русский
			russian_font->draw_text(gc, llpos + 1, l1rpos + 1, line1,CL_Colorf::black);
			russian_font->draw_text(gc, llpos + 1, l2rpos + 1, line2,CL_Colorf::black);
			russian_font->draw_text(gc, llpos + 1, l3rpos + 1, line3,CL_Colorf::black);
			russian_font->draw_text(gc, llpos, l1rpos, line1, CL_Colorf::lightyellow);
			russian_font->draw_text(gc, llpos, l2rpos, line2, CL_Colorf::lightyellow);
			russian_font->draw_text(gc, llpos, l3rpos, line3, CL_Colorf::lightyellow);
			russian_font->draw_text(gc, llpos, l1rpos-0.5f, line1, CL_Colorf::lightyellow);
			russian_font->draw_text(gc, llpos, l2rpos-0.5f, line2, CL_Colorf::lightyellow);
			russian_font->draw_text(gc, llpos, l3rpos-0.5f, line3, CL_Colorf::lightyellow);
		}
		else
		{
			// Японский
			japanese_font->draw_text(gc, llpos + 1, l1jpos + 1, line1,CL_Colorf::black);
			japanese_font->draw_text(gc, llpos + 1, l2jpos + 1, line2,CL_Colorf::black);
			japanese_font->draw_text(gc, llpos + 1, l3jpos + 1, line3,CL_Colorf::black);
			japanese_font->draw_text(gc, llpos, l1jpos, line1, CL_Colorf::lightyellow);
			japanese_font->draw_text(gc, llpos, l2jpos, line2, CL_Colorf::lightyellow);
			japanese_font->draw_text(gc, llpos, l3jpos, line3, CL_Colorf::lightyellow);
			japanese_font->draw_text(gc, llpos, l1jpos-0.5f, line1, CL_Colorf::lightyellow);
			japanese_font->draw_text(gc, llpos, l2jpos-0.5f, line2, CL_Colorf::lightyellow);
			japanese_font->draw_text(gc, llpos, l3jpos-0.5f, line3, CL_Colorf::lightyellow);
		}
	}
	gc.pop_cliprect();
}
